<template>
  <div>
    <div v-if="isMobile" id="mobile-content">
      <div
        style="
          width: 358px;
          height: 422px;
          padding: 16px;
          background: rgba(255, 255, 255, 0.6);
          border-radius: 24px 24px 24px 24px;
          border: 1px solid #ffffff;
          margin-top: 20px;
        ">
        <div
          style="
            height: 23px;
            font-family: Noto Sans SC, Noto Sans SC;
            font-weight: 500;
            font-size: 16px;
            color: #333333;
            line-height: 19px;
            font-style: normal;
            margin: 8px;
            text-transform: none;
          ">
          你好，欢迎登录
        </div>
        <div
          style="
            height: 35px;
            font-family: Noto Sans SC, Noto Sans SC;
            font-weight: bold;
            font-size: 24px;
            color: #333333;
            line-height: 28px;
            font-style: normal;
            margin: 0 0 16px 8px;
            text-transform: none;
          ">
          TAG大数据系统
        </div>
        <el-form
          ref="loginForm"
          :model="loginForm"
          :rules="loginRules"
          class="login-form"
          auto-complete="on"
          label-position="left">
          <el-form-item prop="username">
            <el-input
              ref="username"
              v-model="loginForm.username"
              placeholder="账号"
              name="username"
              type="text"
              tabindex="1"
              auto-complete="on" />
          </el-form-item>

          <el-form-item prop="password">
            <el-input
              :key="passwordType"
              ref="password"
              v-model="loginForm.password"
              :type="passwordType"
              placeholder="密码"
              name="password"
              tabindex="2"
              auto-complete="on"
              @keyup.enter.native="handleLogin" />
          </el-form-item>
          <el-button
            :loading="loading"
            type="primary"
            style="width: 100%; margin-bottom: 30px"
            @click.native.prevent="handleLogin">
            登录
          </el-button>
          <div class="tips">
            <span>{{ loginMsg }}</span>
          </div>
        </el-form>
        <div style="display: flex; justify-content: center; align-items: center; margin-top: 20px">
          <img src="/mobile-hycx.png" width="110" height="36" />
        </div>
        <div style="text-align: center; color: #999; font-size: 12px; margin-top: 10px">最终解释权归慧颜婵欣所有</div>
      </div>
    </div>
    <div v-else id="main-content">
      <div style="padding: 5% 0 0 56%">
        <div style="width: 603px">
          <img src="/main-word.png" width="501" height="116" />
          <div style="width: 100%; height: 614px; background-color: #fff; border-radius: 24px; padding: 27px 45px">
            <div style="font-size: 24px; font-weight: 540; color: #333333; margin-top: 30px">你好，欢迎登录</div>
            <div style="font-size: 36px; font-weight: bold; color: #333333; margin-top: 16px; margin-bottom: 43px">
              TAG大数据系统
            </div>
            <el-form
              ref="loginForm"
              :model="loginForm"
              :rules="loginRules"
              class="login-form"
              auto-complete="on"
              label-position="left">
              <el-form-item prop="username">
                <el-input
                  ref="username"
                  v-model="loginForm.username"
                  placeholder="账号"
                  name="username"
                  type="text"
                  tabindex="1"
                  auto-complete="on" />
              </el-form-item>

              <el-form-item prop="password">
                <el-input
                  :key="passwordType"
                  ref="password"
                  v-model="loginForm.password"
                  :type="passwordType"
                  placeholder="密码"
                  name="password"
                  tabindex="2"
                  auto-complete="on"
                  @keyup.enter.native="handleLogin" />
                <!-- <span class="show-pwd" @click="showPwd">
                  <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
                </span> -->
              </el-form-item>
              <el-button
                :loading="loading"
                type="primary"
                style="width: 100%; margin-bottom: 30px"
                @click.native.prevent="handleLogin">
                登录
              </el-button>
              <div class="tips">
                <span>{{ loginMsg }}</span>
              </div>
            </el-form>
            <div style="display: flex; justify-content: center; align-items: center; margin-top: 20px">
              <div
                style="
                  border-top: 1px solid rgba(153, 153, 153, 0.3);
                  height: 0;
                  width: 163px;
                  margin-right: 20px;
                "></div>
              <img src="/main-yycx.png" width="131" height="43" />
              <div
                style="
                  border-top: 1px solid rgba(153, 153, 153, 0.3);
                  height: 0;
                  width: 163px;
                  margin-left: 20px;
                "></div>
            </div>
            <div style="text-align: center; color: #999; font-size: 12px; margin-top: 10px">
              最终解释权归慧颜婵欣所有
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (value.length < 1) {
        callback(new Error('请输入账号'));
      } else {
        callback();
      }
    };
    const validatePassword = (rule, value, callback) => {
      if (value.length < 1) {
        callback(new Error('请输入密码'));
      } else {
        callback();
      }
    };
    return {
      loginMsg: '',
      loginForm: {
        username: '',
        password: '',
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }],
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  // mounted() {
  //   if (this.$isMobile()) {
  //     // 移动设备逻辑
  //     console.log('This is a mobile device.');
  //   } else {
  //     // 桌面设备逻辑
  //     console.log('This is a desktop device.');
  //   }
  // },
  computed: {
    isMobile() {
      return this.$isMobile();
    },
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = '';
      } else {
        this.passwordType = 'password';
      }
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
    handleLogin() {
      this.loginMsg = '';
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          this.$store
            .dispatch('user/login', this.loginForm)
            .then(res => {
              // if (res) {
              //   this.$router.push('/show/salon/' + res + '?code=SCCD30608001')
              // } else {
              // this.$router.push({ path: this.redirect || '/' })
              // }
              let routePath = '/';
              if ((res >= 200 && res != 203) || (res == 203 && this.isMobile)) {
                routePath = '/mobile';
              }
              this.$router.push({ path: routePath });
              this.loading = false;
            })
            .catch(err => {
              this.loading = false;
              this.loginMsg = '账号密码有误，请核实后重新输入';
              if (err == 400) {
                this.loginMsg = '账号已被冻结，请联系管理员';
              } else if (err.length > 0) {
                this.loginMsg = err;
              }
              this.loginForm.password = '';
            });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
  },
};
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
// $bg:#C31171;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}

#main-content {
  background-image: url('/main-bg.png');
  width: 100%; /* 或者你需要的宽度 */
  height: 100%;
  background-size: cover; /* 背景图片覆盖整个div区域 */
  background-position: center; /* 背景图片居中 */
  background-repeat: no-repeat; /* 背景图片不重复 */
}

#mobile-content {
  background-image: url('/mobile-bg.png');
  width: 100%; /* 或者你需要的宽度 */
  height: 100vh;
  background-size: cover; /* 背景图片覆盖整个div区域 */
  background-position: center; /* 背景图片居中 */
  background-repeat: no-repeat; /* 背景图片不重复 */
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
// $bg:#C31171;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  // background-color: $bg;
  background-image: url('/bg.jpg');
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}

.tips {
  font-size: 14px;
  color: #f56c6c;
  margin-bottom: 10px;
  text-align: center;

  span {
    &:first-of-type {
      margin-right: 16px;
    }
  }
}
</style>
